package per.turtle.file.controller;

import cn.hutool.core.io.IoUtil;
import io.minio.GetObjectResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.file.config.MinioConfig;
import per.turtle.file.service.ISysFileService;
import per.turtle.system.dto.SysFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author turtle
 */
@Api(value = "file", tags = {"file_文件管理模块"})
@RequiredArgsConstructor
@RestController
@Slf4j
public class SysFileController extends BaseController {

    private final ISysFileService sysFileService;

    private final MinioConfig minioConfig;

    @ApiOperation(value = "文件下载", notes = "文件下载")
    @PostMapping("downloadFile")
    public CommonResult<SysFile> downloadFile(HttpServletRequest request, HttpServletResponse response, @RequestBody SysFile file) {
        // 设置响应类型
        response.setCharacterEncoding(request.getCharacterEncoding());
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
        try (GetObjectResponse objectResponse = sysFileService.downloadFile(file)) {
            // 将文件流输出到响应流
            IoUtil.copy(objectResponse, response.getOutputStream());
            response.flushBuffer();
            return null;
        } catch (Exception e) {
            log.error("下载文件失败，" + e.getMessage());
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("upload")
    public CommonResult<SysFile> upload(@RequestPart("file") MultipartFile file, boolean isPub) {
        try {
            // 上传并返回访问地址
            return CommonResult
                    .success(sysFileService.uploadFile(file, isPub ? minioConfig.getBuckets().getPubs().get(0) : minioConfig.getBuckets().getPris().get(0)));
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "文件上传", notes = "文件上传")
    @PostMapping("uploadx")
    public String uploadx(@RequestPart("file") MultipartFile file, boolean isPub) {
        try {
            // 上传并返回访问地址 todo
            SysFile sysFile = sysFileService.uploadFile(file, isPub ? minioConfig.getBuckets().getPubs().get(0) : minioConfig.getBuckets().getPris().get(0));
//            socketServer.sendInfo("yujia", sysFile.getUrl());
            return "成功";
        } catch (Exception e) {
            log.error("上传文件失败", e);
            return "失败";
        }
    }

}